<%@ include file="/inc/header.jsp" %>

	<link rel="StyleSheet" href="inc/dtree.css" type="text/css" />
	<script type="text/javascript" src="inc/dtree.js"></script>

<%!

	private static java.util.List getCategoryListForParent(java.util.List categories, Integer parentId)
	{
		java.util.List subList = new java.util.LinkedList();
		for (int i = 0; i < categories.size(); i++)
		{
			org.docmanagement.forms.CategoryForm cat = (org.docmanagement.forms.CategoryForm) categories.get(i);
			if (cat.getParentId() == null && parentId == null)
				subList.add(cat);
			else if (cat.getParentId() != null && cat.getParentId().equals(parentId))
				subList.add(cat);
		}
		return subList;
	}

	private void showCategories(StringBuffer buffer, java.util.List categories, Integer current, int depth)
	{
		java.util.List subList = getCategoryListForParent(categories, current);
		if (subList.size() == 0)
			return ;
		for (int i = 0; i < subList.size(); i++)
		{
			org.docmanagement.forms.CategoryForm cat = (org.docmanagement.forms.CategoryForm) subList.get(i);
			showCategory(buffer, cat, depth);
			depth++;
			showCategories(buffer, categories, cat.getId(), depth);
		}
	}

	private void showCategory(StringBuffer buffer, org.docmanagement.forms.CategoryForm cat, int depth)
	{
		buffer.append("categorylist.add(");
		// Id
		buffer.append(cat.getId());
		// ParentId
		buffer.append(", ");
		if (cat.getParentId() == null)
			buffer.append(0);
		else
			buffer.append(cat.getParentId());
		// Name
		buffer.append(", '").append(cat.getName()).append("'");
		// Link
		buffer.append(", '").append("element.do?actionName=Lister&id=").append(cat.getId()).append("'");
		// Title
		buffer.append(", ''");
		// Target
		buffer.append(", 'main'");
		// Icon
		buffer.append(", ''");
		// IconOpen
		buffer.append(", ''");
		// isOpen
		buffer.append(", false");
		// End
		buffer.append(");\n");
	}
%>

<script>
<!--

	var categorylist = new dTree('categorylist');

	categorylist.add(0, -1, 'Cat&eacute;gories', 'element.do?actionName=Lister', '', 'main', 'none');
<%
	java.util.List categories = (java.util.List) request.getSession().getAttribute("categorylist");
	if (categories != null)
	{
		StringBuffer buffer = new StringBuffer();
		int depth = 0;
		showCategories(buffer, categories, null, depth);
		
		out.println(buffer.toString());
		//System.out.println(buffer.toString());
	}
%>

	document.write(categorylist);
	categorylist.closeAll();

//-->
</script>

<%@ include file="/inc/footer.jsp" %>
